import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/intro'
  },
  {
    path: '/index',
    name: 'Index',
    component: () => import('@/views/home/Start'),
    children: [
      {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/Login')
      },
      {
        path: '/intro',
        name: 'Intro',
        component: () => import('@/components/home/Introduce')
      },
      {
        path: '/album',
        name: 'album',
        component: () => import('@/components/album/Album')
      },
      {
        path: '/admin',
        name: 'admin',
        component: () => import('@/components/home/AdminConsole')
      },
      {
        path: '/group',
        name: 'group',
        component: () => import('@/components/group/Group')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
//路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/resetPassword' || to.path === '/intro') {
      next()
    } else {
    if (store.getters.getCurrentUser == null) {
      next('/login')
    } else next()
  }
  if (to.meta.title) {
    document.title = to.meta.title;
  }
})
export default router
